// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.webviewflutter;

import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.plugin.common.PluginRegistry;
import android.content.Intent;
import android.app.Activity;
import android.content.Context;
import io.flutter.plugins.webviewflutter.FlutterWebView;
import android.content.Intent;
import android.net.Uri;


/** WebViewFlutterPlugin */
public class WebViewFlutterPlugin implements PluginRegistry.ActivityResultListener{
  /** Plugin registration. */
  private Activity activity;
  private Context context;

  //add for file
  // private ValueCallback<Uri> mUploadMessage;
  // private ValueCallback<Uri[]> mUploadMessageArray;
  private final static int FILECHOOSER_RESULTCODE=1;
  // private Uri fileUri;
  // private Uri videoUri;
  private static WebViewFactory factory;

  private WebViewFactory factory2;
  public static void registerWith(Registrar registrar) {
    factory = new WebViewFactory(registrar.messenger(), registrar.view());
    registrar
        .platformViewRegistry()
        .registerViewFactory(
            "plugins.flutter.io/webview",
            factory);
    
    final WebViewFlutterPlugin instance = new WebViewFlutterPlugin(registrar.activity(),registrar.activeContext());
    registrar.addActivityResultListener(instance);
    FlutterCookieManager.registerWith(registrar.messenger());
  }

  private WebViewFlutterPlugin(Activity activity, Context context){
    this.activity = activity;
    this.context = context;
  }

  @Override
    public boolean onActivityResult(int i, int i1, Intent intent) {
      
        if(factory!=null){
          factory2 = factory;
          System.out.println(factory2.getFlutterWebView() != null);
          if (factory2.getFlutterWebView() != null) {
            return factory2.getFlutterWebView().resultHandler.handleResult(i, i1, intent);
          }
          return false;
        }else{
          WebViewFactory flutterWebViewFactory = new WebViewFactory(null,null);
          if(flutterWebViewFactory!=null){
            return flutterWebViewFactory.getFlutterWebView().resultHandler.handleResult(i, i1, intent);
          }
          return false;
        }
        
    }
}

